package com.stylefeng.guns.modular.fileManage.warpper;

import com.stylefeng.guns.common.persistence.dao.ZyTeaPictureMapper;
import com.stylefeng.guns.common.persistence.model.ZyPicture;
import com.stylefeng.guns.common.persistence.model.ZyTeaPicture;
import com.stylefeng.guns.core.base.warpper.BaseControllerWarpper;
import com.stylefeng.guns.modular.fileManage.dao.ZyBrewsPictureDao;
import com.stylefeng.guns.modular.fileManage.dao.ZyTeaPictureDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 图片管理的包装类
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:47:03
 */
public class PictureWarpper extends BaseControllerWarpper {

    @Autowired
    private Environment env;

    @Value("${server.context-path}")
    private String contextPath;

    private ZyTeaPictureDao teaPictureDao;

    private ZyBrewsPictureDao brewsPictureDao;

    private Integer teaId;

    private List<Integer> picIds;

    private ZyTeaPicture teaPicture;

    public PictureWarpper(List<ZyPicture> list) {
        super(list);
    }

    /**
     * 前台扫码界面包装茶叶图片
     *
     * @param list
     * @param teaId
     * @param teaPictureDao
     */
    public PictureWarpper(List<ZyPicture> list, Integer teaId, ZyTeaPictureDao teaPictureDao) {
        super(list);
        this.teaId = teaId;
        this.teaPictureDao = teaPictureDao;
    }

    /**
     * 显示已关联图片
     *
     * @param picIds 关联的图片id集合
     */
    public PictureWarpper(List<ZyPicture> list, List<Integer> picIds) {
        super(list);
        this.picIds = picIds;
    }

    /**
     * 包装单个picture对象
     */
    public PictureWarpper(ZyPicture pic) {
        super(pic);
    }

    /**
     * 包装单个picture对象
     */
    public PictureWarpper(ZyPicture pic, ZyTeaPicture teaPicture, ZyTeaPictureDao teaPictureDao) {
        super(pic);
        this.teaPicture = teaPicture;
        this.teaPictureDao = teaPictureDao;
    }

    @Override
    public void warpTheMap(Map<String, Object> map) {
        //图片的src地址
        map.put("src", getSrc((String) map.get("savePath"), (String) map.get("saveName")));
        Integer id;
        Object idObject = map.get("id");
        if (idObject instanceof Integer) {
            id = (Integer) idObject;
        } else {
            id = Integer.valueOf((String) idObject);
        }
        if (teaPicture != null) {
            //图片与茶叶对应表的label字段
            map.put("label", teaPicture.getLabel());
            //获得排序字段信息
            map.put("sequence", teaPicture.getLabel());
        }
        if(brewsPictureDao != null){
            map.put("sequence", getBrewsSequence(id));
        }
        //包装图片的“已选”状态
        if (picIds != null) {
            map.put("isChecked", getChecked(id));
        }
    }

    /**
     * 如果图片id在ids集合中则返回true
     *
     * @param pictureId
     * @return
     */
    private Boolean getChecked(Integer pictureId) {
        for (Integer id : picIds) {
            if (pictureId.equals(id)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 得到冲泡方式图片的sequence字段
     * @param pictureId
     * @return
     */
    private String getBrewsSequence(Integer pictureId){

        return null;
    }

    /**
     * 得到图片的访问地址
     *
     * @param savePath
     * @param saveName
     * @return
     */
    private String getSrc(String savePath, String saveName) {
        StringBuffer sb = new StringBuffer("/admin/static/");
        sb.append(savePath.substring(savePath.indexOf("img")));
        if (!sb.toString().endsWith(File.separator) && !sb.toString().endsWith("/")) {
            sb = new StringBuffer(sb.append("/"));
        }
        sb.append(saveName);
        return sb.toString();
    }

}
